/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.FacesConverter;
import javax.faces.convert.Converter;
import util.enumeration.AccessRight;

/**
 *
 * @author WenYong
 */
@FacesConverter(value = "accessRightsConverter", forClass = util.enumeration.AccessRight.class)
public class AccessRightsConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        if (value == null || value.length() == 0) {
            return null;
        }

        if (value.equals("ADD_TCUBE_USER")) {
            return AccessRight.ADD_TCUBE_USER;
        } else if (value.equals("EDIT_ACCESS_RIGHTS")) {
            return AccessRight.EDIT_ACCESS_RIGHTS;
        } else if (value.equals("UNLOCK_ACCOUNTS")) {
            return AccessRight.UNLOCK_ACCOUNTS;
        } else if (value.equals("DELETE_TCUBE_USER")) {
            return AccessRight.DELETE_TCUBE_USER;
        } else if (value.equals("INACTIVATE_ACCOUNTS")) {
            return AccessRight.INACTIVATE_ACCOUNTS;
        } else if (value.equals("VERIFY_STAKEHOLDER_ACCOUNTS")) {
            return AccessRight.VERIFY_STAKEHOLDER_ACCOUNTS;
        } else if (value.equals("MANAGE_MAILBOX")) {
            return AccessRight.MANAGE_MAILBOX;
        } else {
            return AccessRight.VIEW_ALL_STAFF;
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (value == null) {
            return null;
        }

        if (value instanceof AccessRight) {
            AccessRight accessRights = (AccessRight) value;

            if (accessRights.equals(AccessRight.ADD_TCUBE_USER)) {
                return "ADD_TCUBE_USER";
            }
            if (accessRights.equals(AccessRight.EDIT_ACCESS_RIGHTS)) {
                return "EDIT_ACCESS_RIGHTS";
            }
            if (accessRights.equals(AccessRight.DELETE_TCUBE_USER)) {
                return "DELETE_TCUBE_USER";
            }
            if (accessRights.equals(AccessRight.INACTIVATE_ACCOUNTS)) {
                return "INACTIVATE_ACCOUNTS";
            }
            if (accessRights.equals(AccessRight.UNLOCK_ACCOUNTS)) {
                return "UNLOCK_ACCOUNTS";
            }
            if (accessRights.equals(AccessRight.VERIFY_STAKEHOLDER_ACCOUNTS)) {
                return "VERIFY_STAKEHOLDER_ACCOUNTS";
            }
            if (accessRights.equals(AccessRight.MANAGE_MAILBOX)) {
                return "MANAGE_MAILBOX";
            } else {
                return "VIEW_ALL_STAFF";
            }
        } else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: util.enumeration.AccessRights");
        }
    }
}
